﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for UpdateCustomer.xaml
    /// </summary>
    public partial class UpdateCustomer : Window
    {
        private Customer customer;
        DienThoaiDataContext db = new DienThoaiDataContext();
        public UpdateCustomer(Customer customer)
        {
            InitializeComponent();
            this.customer = customer;

            foreach (var i in db.Kinds)
            {
                cbbType.Items.Add(i.Detail);
            }
            
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbMaKH.Content = customer.PersonID.Trim();
            txtFullName.Text = customer.Person.Fullname.Trim();
            dpDOB.SelectedDate = customer.Person.DOB;
            txtAddress.Text = customer.Person.Address.Trim();
            txtPhone.Text = customer.Person.Phone.Trim();
            cbbType.SelectedValue = customer.Kind.Detail;
        }

        private void btEdit_Click(object sender, RoutedEventArgs e)
        {
           // var updateCustomer = from i in db.Customers
            float number ;
            if (txtFullName.Text == "")
                MessageBox.Show("Bạn chưa điền họ tên.");
            else if (txtAddress.Text == "")
                MessageBox.Show("Bạn chưa điền địa chỉ.");
            else if (dpDOB.SelectedDate == null)
                MessageBox.Show("Bạn chưa chọn ngày sinh.");

            else if ((float.TryParse(txtPhone.Text, out number)) == false && txtPhone.Text != "")
                MessageBox.Show("Số điện thoại có chứa ký tự. Xin nhập lại.");
            else
            {
                var updateCustomer = from i in db.Customers
                                     where i.PersonID == customer.PersonID
                                     select i;

                var kindID = from k in db.Kinds
                             where k.Detail == cbbType.SelectedValue.ToString()
                             select k.KindID;

                foreach (var i in updateCustomer)
                {
                    i.Person.Address = txtAddress.Text;
                    i.Person.Fullname = txtFullName.Text;
                    i.Person.Address = txtAddress.Text;
                    i.Person.DOB = dpDOB.SelectedDate;
                    i.Person.Phone = txtPhone.Text;
                    foreach (var j in kindID)
                        i.KindID = j;
                    db.SubmitChanges();
                    MessageBox.Show("Khách hàng " + txtFullName.Text + " đã sửa vào CSDL.");
                    DialogResult = true;
                }
            }
            
        }
    }
}
